﻿using Base.FileRemoteModel.Pdf.Model;
using Base.HrExtendService;
using Basic.HrCollect;
using Basic.HrModel.SignatureFile;
using Basic.HrRemoteModel;
using Basic.HrRemoteModel.EmpSignatureFile.Model;
using Basic.HrRemoteModel.PdfSign.Model;
using Basic.HrService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.HrService.lmpl
{
    internal class EmpSignatureFileService : IEmpSignatureFileService
    {
        private readonly IEmpSignatureFileCollect _Service;
        private readonly IHrEmpSignConfig _Config;
        private readonly IFileService _File;
        public EmpSignatureFileService ( IEmpSignatureFileCollect service, IHrEmpSignConfig config, IFileService file )
        {
            this._File = file;
            this._Config = config;
            this._Service = service;
        }
        public SignFileItem[] Gets ( long empId )
        {
            return this._Service.Gets(empId);
        }
        public SignResult PdfSign ( long empId, WriteSignPdf obj )
        {
            long[] signId = obj.Sign.ConvertAll(c => c.SignFileId);
            SignatureFileDto[] files = this._Service.Gets<SignatureFileDto>(signId);
            files.ForEach(c =>
            {
                c.CertPwd = this._Config.Decrypt(c.CertPwd);
            });
            WritePdfFileSign sign = new WritePdfFileSign
            {
                EmpId = empId,
                PdfFileId = obj.PdfFileId,
                DirKey = obj.DirKey,
                Height = obj.Height,
                LinkBizPk = obj.LinkBizPk,
                Tag = obj.Tag,
                Width = obj.Width,
                UserType = "emp",
                Sign = obj.Sign.ConvertAll(c =>
                {
                    SignatureFileDto dto = files.Find(f => f.Id == c.SignFileId);
                    return new WritePdfSign
                    {
                        CertPwd = dto.CertPwd,
                        FileId = c.FileId,
                        SignFileId = dto.FileId,
                        Height = c.Height,
                        Index = c.Index,
                        Width = c.Width,
                        Location = c.Location,
                        Reason = c.Reason,
                        SignType = dto.EncryptionType == HrCertEncryptionType.SM2 ? WritePdfSignType.SM2 : WritePdfSignType.RSA,
                        X = c.X,
                        Y = c.Y,
                    };
                })
            };
            FilePdfResult result = this._File.SavePdfSignFile(sign);
            return new SignResult
            {
                FileId = result.FileId,
                FileUri = result.FileUri,
                Extension = result.Extension,
                FileName = result.FileName
            };
        }
        public void CheckPwd ( long id, long empId, string pwd )
        {
            SignFilePwd obj = this._Service.GetPwd(id);
            if ( obj.IsDrop )
            {
                throw new ErrorException("hr.emp.sign.file.not.find");
            }
            else if ( obj.EmpId != empId )
            {
                throw new ErrorException("hr.emp.no.power");
            }
            string oldPwd = this._Config.Decrypt(obj.CertPwd);
            if ( oldPwd != pwd )
            {
                throw new ErrorException("hr.emp.sign.file.pwd.error");
            }
        }

        public void Drop ( long id, long empId )
        {
            EmpSignatureFile file = this._Service.Get(id);
            if ( file.EmpId != empId )
            {
                throw new ErrorException("hr.emp.no.power");
            }
            this._Service.Drop(file);
        }

        public PagingResult<SignFileDto> Query ( long empId, SignQueryParam query, IBasicPage paging )
        {
            SignFileDto[] list = this._Service.Query<SignFileDto>(empId, query, paging, out int count);
            return new PagingResult<SignFileDto>(list, count);
        }
    }
}
